package pl.jtb.ctl.tabory.ui.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigesterUtil {

    private static DigesterUtil instance;

    private DigesterUtil() {

    }

    public static DigesterUtil getInstance() {
        if (instance == null) {
            instance = new DigesterUtil();
        }
        return instance;
    }

    public String getHash(String string) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        StringBuffer buffer = new StringBuffer();
        MessageDigest digester = MessageDigest.getInstance("MD5");
        byte[] patternDigest = digester.digest(string.getBytes("UTF-8"));
        for (int i = 0; i < patternDigest.length; i++) {
            buffer.append(Integer.toString((patternDigest[i] & 0xff) + 0x100, 16).substring(1));
        }
        return buffer.toString();
    }

}
